;menu code

;main menu main loop
mmenu_main_loop:
	ld		a,5
	ld		[number_menu_options],a				;number of menu options
	xor		a
	ld		[action_number_set],a				;action  number set
	call	mmenu_keyboard_polling
	
	halt
	
	;call	REPRODUCE_EFECTO
	
	jr		mmenu_main_loop

	ret



mmenu_load_save_main_loop:

	ld		a,3		
	ld		[number_menu_options],a				;number of menu options
	ld		a,1								
	ld		[action_number_set],a				;action number set 
	call	mmenu_keyboard_polling
	
	halt
	
	;call	REPRODUCE_EFECTO
	
	jr		mmenu_load_save_main_loop

	ret


;main menu routines	
;if mmenu_option<5 then mmenu_position=mmenu_option+1 else mmenu_option=0
;a = number of items
mmenu_update_next_option:
	ld		b,a
	dec		b
	ld		a,[mmenu_option]
	cp		b
	jr		z,mmenu_update_next_option_reset
	inc		a
	ld		[mmenu_option],a
	ret

mmenu_update_next_option_reset:
	xor		a
	ld		[mmenu_option],a	
	ret
	

	
;a = number of items	
mmenu_update_prev_option:
	ld		b,a
	dec		b
	ld		a,[mmenu_option]
	and		a									;0?
	jr		z,mmenu_update_prev_option_reset
	dec		a
	ld		[mmenu_option],a
	ret

mmenu_update_prev_option_reset:
	ld		a,b
	ld		[mmenu_option],a	
	ret
	
	

mmenu_perform_action:
	ld		a,[action_number_set]
	and		a
	
	ld		a,[mmenu_option]	
	jr		nz,mmenu_perform_action_saveload

	IF DEMO_MODE==0
	cp		0
	jr.		z,story_mode_setup
	ENDIF
	
	cp		1
	jr.		z,mmenu_init_shop
		
	
	cp		2
	jr.		z,battle_training_setup
	
	IF DEMO_MODE==0
	cp		3
	jr.		z,mmenu_save
	
	cp		4
	jr.		z,mmenu_load
	ENDIF
	IF DEMO_MODE==1
	ret
	ENDIF
	
mmenu_perform_action_saveload:

	cp		0
	jr		z,mmenu_tape_action
	
	cp		1
	jr.		z,mmenu_sram_action
	
	cp		2							;goto main menu
	jr		z,mmenu_saveload_back
	
	
	

mmenu_saveload_back:
	call	menu_clean_menu

	;reset menu option
	ld		a,[menu_save_or_load]
	and		a			;a==0 -> save	
	jr		nz,mmenu_saveload_back_load
	;back to save
	ld		a,3
	jr		mmenu_saveload_back_load_after
mmenu_saveload_back_load
	;back to load
	ld		a,4	
mmenu_saveload_back_load_after:
	ld		[mmenu_option],a
		
	call	main_menu_display_options
	ld		a,3
	call	mmenu_display_cursor	
	
	;stack must point at 0xF380
	pop		af
	pop		af
	pop		af
	pop		af
	
	jr.		mmenu_main_loop
	
	
	
mmenu_tape_action:
	ld		a,[menu_save_or_load]
	and		a			;a==0 -> save	
	jr		nz,mmenu_tape_action_load
	;save tape
	call	deck_status2buffer
	;stop music & fx
	call	PLAYER_OFF
	call	savegame_on_tape
	
	and		a
	jr		z,mmenu_tape_action_no_error
	ld		hl,main_menu_saveload_error
	jr		mmenu_tape_action_write_status
mmenu_tape_action_no_error:
	ld		hl,main_menu_saveload_saved
mmenu_tape_action_write_status:	
	ld		de,NAMTBL + MMENU_FIRST_OPTION_START + (32*7)
	ld		bc,6
	call	0x005c
	
	;start music
	ld		a,TUNE_MMENU
	call	start_music
			
	ld		b,100
mmenu_tape_action_save_delay:
	halt	
	djnz	mmenu_tape_action_save_delay	

	call	remove_loadsave_status			
			
	ret

mmenu_tape_action_load:
	;load tape
	
	;stop music & fx
	call	PLAYER_OFF
	call	loadgame_from_tape

	push	af

	call	buffer2deck_status

	pop		bc
	or		b											;a or b (error1 or error2 !=0 => error)

	and		a
	jr		z,mmenu_tape_action_no_error_load
	ld		hl,main_menu_saveload_error
	jr		mmenu_tape_action_write_status_load
mmenu_tape_action_no_error_load:
	ld		hl,main_menu_saveload_loaded
mmenu_tape_action_write_status_load:	
	ld		de,NAMTBL + MMENU_FIRST_OPTION_START + (32*7)
	ld		bc,6
	call	0x005c

	;start music
	ld		a,TUNE_MMENU
	call	start_music

	ld		b,100
mmenu_tape_action_load_delay:
	halt	
	djnz	mmenu_tape_action_load_delay	

	call	remove_loadsave_status


	call	shop_init_counters							;recalculates counters	
	ret
	
	
	
mmenu_sram_action:
	ld		a,[menu_save_or_load]
	and		a			;a==0 -> save	
	jr		nz,mmenu_sram_action_load
	;save sram
	ld		a,[sram_found]
	inc		a											;zero if found
	jr		z,mmenu_sram_action_no_error_save
	ld		hl,main_menu_saveload_nofound
	jr		mmenu_sram_action_write_status_save
mmenu_sram_action_no_error_save:
	ld		hl,main_menu_saveload_saved
mmenu_sram_action_write_status_save:	
	ld		de,NAMTBL + MMENU_FIRST_OPTION_START + (32*7)
	ld		bc,10
	call	0x005c
		
	call	deck_status2buffer
	call	savegame_on_sram
	
	
	ld		b,100
mmenu_sram_action_save_delay:
	halt	
	djnz	mmenu_sram_action_save_delay	

	call	remove_loadsave_status	
	
	ret

mmenu_sram_action_load:	
	;load sram
	ld		a,[sram_found]
	inc		a
	jr		nz,mmenu_sram_action_load_notfound
	
	call	loadgame_from_sram
	call	buffer2deck_status
	and		a
	jr		nz,mmenu_sram_action_load_error
	ld		hl,main_menu_saveload_loaded
	jr		mmenu_sram_action_load_print_status

mmenu_sram_action_load_notfound:
	ld		hl,main_menu_saveload_nofound
	jr		mmenu_sram_action_load_print_status
mmenu_sram_action_load_error:	
	ld		hl,main_menu_saveload_error
	
mmenu_sram_action_load_print_status:
	ld		de,NAMTBL + MMENU_FIRST_OPTION_START + (32*7)
	ld		bc,10
	call	0x005c

	ld		b,100
mmenu_sram_action_load_delay:
	halt	
	djnz	mmenu_sram_action_load_delay
	
	call	remove_loadsave_status	

	call	shop_init_counters							;recalculates counters

	ret
	
	
mmenu_load:	
	xor		a
	ld		[mmenu_option],a			;set first option	
	ld		a,1
	ld		[menu_save_or_load],a
	call	menu_clean_menu
	call	init_save_load_menu
	
	jr.		mmenu_load_save_main_loop


mmenu_save:
	xor		a
	ld		[menu_save_or_load],a
	ld		[mmenu_option],a			;set first option
	
	call	menu_clean_menu
	call	init_save_load_menu

	jr.		mmenu_load_save_main_loop


mmenu_init_shop:
	call	PLAYER_OFF
	call	cls_screen
	call	shop_load_patterns
	call	shop_load_topcard_template
	call	shop_display_init_screen
	
	call	init_shop_fire_fx
	call	set_shop_sprites
	
	ld		a,TUNE_SHOP
	call	start_music
	
	IF DEMO_MODE==1
	ld		hl,player_deck_status
	xor		a
	ld		[hl],a
	ld		hl,player_deck_status+94
	ld		a,0xc0	
	ld		[hl],a	
	ENDIF
	call	shop_main_loop
	ret

	
clear_shop_card_deck_buffer:
	ld		hl,shop_card_deck_buffer
	xor		a
	ld		[hl],a	
	ld		de,shop_card_deck_buffer+1
	ld		bc,300
	ldir
	ret
	
	

menu_clean_menu:
	ld		b,10
	ld		hl,NAMTBL + MMENU_CURSOR_START
	ld		de,32
menu_clean_menu_loop:
	push	bc
		
	xor		a
	ld		bc,13
	call	0x0056

	add		hl,de					;next line
	
	pop		bc
	djnz	menu_clean_menu_loop


	ret
